// 初始化数据模块
import http from '@/anaf/request/index'


import {
	CONFIG,
	TEMPLATE,
	SHARE_INFO,
	PAGE_ROUTES,
} from '../types.js'



const state = {
	config: uni.getStorageSync('config') ? uni.getStorageSync('config') : {},
	routes: [],
	template: uni.getStorageSync('template') ? uni.getStorageSync('template') : {},
	hasTemplate: true, //是否有模板数据
	shareInfo: {},
	

}

const actions = {

	appInit({
		commit
	}, options) {
		return new Promise((resolve, reject) => {
			
		})
	},

	

	
}

const mutations = {
	[CONFIG](state, data) {
		let sysc  = uni.getStorageSync('sysconfig');
		if(sysc==''){
			// this.$api.msg("配置获取失败")
			getsys()
		}else{
			let sysj = JSON.parse(sysc)
			console.log(sysj)
			let t = Math.floor(Date.now() / 1000);
			console.log(t)
			console.log(t>sysj.exp)
			// 如果已经超时缓存
			if(t>sysj.exp){
				getsys()
			}
		}
		// state.config = data
		// uni.setStorageSync('config', data);
	},
	[TEMPLATE](state, data) {
		state.template = data
	},
	[PAGE_ROUTES](state, data) {
		state.routes = data
	},
	[SHARE_INFO](state, data) {
		state.shareInfo = data
	},
	hasTemplate(state, data) {
		state.hasTemplate = data
	},
	// 弹窗一次的话，关闭的时候删除数据。
	delPopup(state, index) {
		let templateData = state.template;
		uni.removeStorageSync('template');
		templateData.popup[0].content.list.splice(index, 1)
		uni.setStorageSync('template', templateData);
		state.template = templateData;
	},
	getsys(state){
		http('common.sysconfig', {}, '').then(res => {
			if (res.status == 200) {
				let sd = {}
				sd.exp = Math.floor(Date.now() / 1000)+86400; //过期时间为1天
				// sd.exp = Math.floor(Date.now() / 1000)+10; //过期时间为1天
				sd.data = res.data
				uni.setStorageSync('sysconfig',JSON.stringify(sd));
				state.config = res.data
			}
		})
	},
}

const getters = {

}

export default {
	state,
	mutations,
	actions,
	getters
}
